<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\common\model\room;

use app\common\model\BaseModel;
use app\common\model\Building;
use app\common\model\Campus;
use app\common\model\user\User;


/**
 * 公寓房间管理模型
 * Class RoomApartment
 * @package app\common\model\room
 */
class RoomApartment extends BaseModel {

    protected $name = 'room_apartment';

    public static function getTypeList($val = null) {
        $arr = [
            "0" => "空房",
            "1" => "在职",
            "2" => "离退休",
            "3" => "家属",
        ];
        if (is_numeric($val)) {
            return $arr[$val] ?? '';
        }
        return $arr;
    }

    public static function getLiftList($val = null) {
        $arr = [
            '0' => '免费',
            '50' => '分摊50%',
            '100' => '分摊100%',
        ];
        if (is_numeric($val)) {
            return $arr[$val] ?? '';
        }
        return $arr;
    }

    public function campus() {
        return $this->hasOne(Campus::class, 'id', 'cid');
    }

    public function building() {
        return $this->hasOne(Building::class, 'id', 'bid');
    }

    public function user() {
        return $this->hasMany(RoomApartmentUserRecords::class, 'room_apartment_id', 'id');
    }

    public function allUserInfo() {
        return $this->hasMany(RoomApartmentUserRecords::class, 'room_apartment_id', 'id');
    }

    public function allUser() {
        return $this->hasMany(RoomApartmentUserRecords::class, 'room_apartment_id', 'id')->with([
            "user"
        ]);
    }

    public function allDevice() {
        return $this->hasMany(RoomApartmentDevice::class, 'rid', 'id');
    }

    public function allDeviceLog() {
        return $this->hasMany(RoomApartmentDeviceLog::class, 'rid', 'id');
    }

    // 住户报表数据查询
    public function userRoomDeviceStat() {
        return $this->hasMany(RoomApartmentDevice::class, 'rid', 'id');
    }

    public function getSortAttr($val, $item) {
        return $this->handle_sort($item['sort']);
    }

    public function getAllUserInfoAttr($val, $item) {
        if (empty($item['user_ids'])) {
            $item['user_ids'] = [];
        } else {
            $item['user_ids'] = explode(",", $item['user_ids']);
            foreach ($item['user_ids'] as &$v) {
                $v = (int)$v;
            }
        }
        return User::where([
            ["id", "in", $item['user_ids']],
        ])->select()->toArray();
    }

    public function getUserIdsAttr($val, $item) {
        if (!empty($item['no_append'])) {
            // 如果禁止append，则不做处理
            return $item['user_ids'];
        }
        if (empty($item['user_ids'])) {
            $item['user_ids'] = [];
        } else {
            $item['user_ids'] = explode(",", $item['user_ids']);
            foreach ($item['user_ids'] as &$v) {
                $v = (int)$v;
            }
        }
        return $item['user_ids'];
    }

    public function getAllDeviceAttr($value, $data) {
        $data['room_ids'] = empty($data['room_ids']) ? [] : explode(',', $data['room_ids']);
        $lists = RoomApartmentDevice::where([
            ["rid", "in", $data['room_ids']],
        ])->with(["campus", "building", "room", "device"])->select()->toArray();
        foreach ($lists as &$v) {
            $v['s_use_total'] = $v['s_price'] = $v['s_total_fee'] = $v['d_use_total'] = $v['d_price'] = $v['d_total_fee'] = 0;
            if ($v['device']['type'] == 1) {
                $v['s_use_total'] = $v['use_total'];
                $v['s_price'] = $v['price'];
                $v['s_total_fee'] = $v['total_fee'];
            } elseif ($v['device']['type'] == 2) {
                $v['d_use_total'] = $v['use_total'];
                $v['d_price'] = $v['price'];
                $v['d_total_fee'] = $v['total_fee'];
            }
        }
        return $lists;
    }

    public function getAllDeviceLogAttr($value, $data) {
        $room_ids = empty($data['room_ids']) ? [] : explode(',', $data['room_ids']);
        return RoomApartmentDeviceLog::field([
            "'" . count($room_ids) . "' as room_count",
            "sum( IF( device_type=1, use_total, 0) ) as all_s_use_total",
            "sum( IF( device_type=1, total_fee, 0) ) as all_s_total_fee",
            "sum( IF( device_type=2, use_total, 0) ) as all_d_use_total",
            "sum( IF( device_type=2, total_fee, 0) ) as all_d_total_fee",
            "( sum( total_fee ) + " . $data['all_lift_fee'] . " + " . $data['all_second_pressure_fee'] . " ) as all_total",
        ])->where([
            ["rid", "in", $room_ids],
        ])->find()->toArray();
    }

    public function getCbIdsAttr($value, $data) {
        return [$data['cid'], $data['bid']];
    }

    public function getTypeTextAttr($value, $data) {
        return self::getTypeList($data['type']);
    }

    public function getLiftTypeTextAttr($value, $data) {
        if ($data['lift_type'] == '100') {
            $return_text = "平摊";
        } elseif ($data['lift_type'] == '0') {
            $return_text = "免费";
        } else {
            $return_text = $data['lift_type'] . "%";
        }
        return "分摊类型：" . $return_text;
    }

    public static function getPrintKeywordListRoom() {
        return [
            "index" => "序号",
            "name" => "房间信息",
            "room_d_use_total" => "总用电量",
            "room_d_price" => "平均电价",
            "room_d_total_fee" => "总电费",
            "room_s_use_total" => "总用水量",
            "room_s_price" => "平均水价",
            "room_s_total_fee" => "总水费",
            "room_lift_fee" => "电梯费",
            "room_second_pressure_fee" => "二次加压费",
            "room_all_total_fee" => "总费用",
        ];
    }

    public function getPrintKeywordListUserStat() {
        return [
            "user_name" => "姓名",
            "all_d_total_fee" => "电费",
            "all_s_total_fee" => "水费",
            "date_time" => "日期",
        ];
    }

    public function getPrintKeywordListUserDevice() {
        return [
            "room_name" => "房间名",
            "user_name" => "姓名",

            "d_base_number" => "电表底数",
            "d_reading" => "电表读数",
            "d_use_total" => "用电度数",
            "d_price" => "电单价",
            "d_total_fee" => "电费",

            "s_base_number" => "水表底数",
            "s_reading" => "水表读数",
            "s_use_total" => "用水度数",
            "s_price" => "水单价",
            "s_total_fee" => "水费",

            "lift_fee" => "电梯费",
            "second_pressure_fee" => "加压费",

            "month_time" => "月份",
        ];
    }

}
